package com.jhgsys.internal.pm.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.pm.entity.DemoBook;
import com.jhgsys.internal.pm.service.IDemoBookService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 示例-图书 Controller
 *
 * @author chenweihua
 * @date 2022-02-05 11:07:44
 */
@Slf4j
@Validated
@Controller
public class DemoBookController extends BaseController {

    @Autowired
    private IDemoBookService demoBookService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "demoBook")
    @RequiresPermissions("demoBook:view")
    public String demoBookIndex(){
        return FebsUtil.view("demo/demoBook/demoBookList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "demoBook/add")
    @RequiresPermissions("demoBook:view")
    public String demoBookAdd(){
        return FebsUtil.view("demo/demoBook/demoBookEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "demoBook/update/{id}")
    @RequiresPermissions("demoBook:view")
    public String demoBookUpdate(Model model, @PathVariable("id") Integer id){
        model.addAttribute("demoBook", demoBookService.findDemoBookById(id));
        return FebsUtil.view("demo/demoBook/demoBookEdit");
    }

    /**
     * 查询（所有）
     * @author chenweihua
     * @date 2022-02-05 11:07:44
     */
    @GetMapping("demoBook")
    @ResponseBody
    @RequiresPermissions("demoBook:view")
    public FebsResponse getAllDemoBooks(DemoBook demoBook) {
        return new FebsResponse().success().data(demoBookService.findDemoBooks(demoBook));
    }

    /**
     * 查询（分页）
     * @author chenweihua
     * @date 2022-02-05 11:07:44
     */
    @GetMapping("demoBook/list")
    @ResponseBody
    @RequiresPermissions("demoBook:view")
    public FebsResponse demoBookList(QueryRequest request, DemoBook demoBook) {
        Map<String, Object> dataTable = getDataTable(this.demoBookService.findDemoBooks(request, demoBook));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 DemoBook
     * @author chenweihua
     * @date 2022-02-05 11:07:44
     */
    @Log("新增DemoBook")
    @PostMapping("demoBook")
    @ResponseBody
    @RequiresPermissions("demoBook:view")
    public FebsResponse addDemoBook(@Valid DemoBook demoBook) throws FebsException {
        try {
            demoBook.setCreatedBy(getCurrentUser().getUsername());
            this.demoBookService.createDemoBook(demoBook);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增DemoBook失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 DemoBook
     * @author chenweihua
     * @date 2022-02-05 11:07:44
     */
    @Log("逻辑删除DemoBook")
    @GetMapping("demoBook/delete/{id}")
    @ResponseBody
    @RequiresPermissions("demoBook:view")
    public FebsResponse deleteDemoBook(@PathVariable("id") String demoBookIds) throws FebsException {
        try {
            DemoBook demoBook = new DemoBook();
            demoBook.setDeletedBy(getCurrentUser().getUsername());
            demoBook.setDeletedTime(new Date());
            demoBook.setDeletedFlg((byte)1);
            this.demoBookService.deleteDemoBook(demoBook,demoBookIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除DemoBook失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 DemoBook
    * @author chenweihua
    * @date 2022-02-05 11:07:44
    */
    @Log("物理删除DemoBook")
    @GetMapping("demoBook/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("demoBook:view")
    public FebsResponse physicallyDeleteDemoBook(@PathVariable("id") String demoBookIds) throws FebsException {
        try {
            this.demoBookService.physicallyDeleteDemoBook(demoBookIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除DemoBook失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 DemoBook
     * @author chenweihua
     * @date 2022-02-05 11:07:44
     */
    @Log("修改DemoBook")
    @PostMapping("demoBook/update")
    @ResponseBody
    @RequiresPermissions("demoBook:view")
    public FebsResponse updateDemoBook(DemoBook demoBook) throws FebsException {
        try {
            demoBook.setUpdatedBy(getCurrentUser().getUsername());
            this.demoBookService.updateDemoBook(demoBook);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改DemoBook失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 导出 DemoBook的excel
     * @author chenweihua
     * @date 2022-02-05 11:07:44
     */
    @PostMapping("demoBook/excel")
    @ResponseBody
    @RequiresPermissions("demoBook:view")
    public void export(QueryRequest queryRequest, DemoBook demoBook, HttpServletResponse response) throws FebsException {
        try {
            List<DemoBook> demoBooks = this.demoBookService.findDemoBooks(queryRequest, demoBook).getRecords();
            ExcelKit.$Export(DemoBook.class, response).downXlsx(demoBooks, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
